<template>
  <div class="login-container">
    <div class="login-body">
      <div class="left-container">
        <img src="../assets/login_2.png" alt="">
      </div>
      <div class="right-container">
        <h2 class="title">贵州省疫情精准防控大数据综合管理平台</h2>
          <div class="title-container">
            <h3 class="sub-title">
              用户登录
            </h3>
          </div>
          <div class="login-content">
            <div class="item">
              <span class="icon">
                <img src="../assets/login_3.png" alt="user">
              </span>
              <input 
                class="input" 
                :disabled="!canSend"
                type="text" 
                placeholder="请输入登录账号"
                autocomplete="off"
                v-model.trim="loginForm.userName"/>
            </div>
            <div class="item">
              <span class="icon">
                <img src="../assets/login_4.png" alt="user">
              </span>
              <input 
              class="input" 
              :disabled="!canSend"
              type="password" 
              placeholder="请输入登录密码"
              autocomplete="off"
              v-model.trim="loginForm.password"/>
            </div>
            <div class="item">
              <span class="icon">
                <img src="../assets/login_5.png" alt="user">
              </span>
              <input 
                :disabled="!canSend"
                class="input" 
                type="text" 
                placeholder="请输入图片验证码"
                autocomplete="off"
                v-model.trim="loginForm.code"/>
              <img 
                :src="imageCode"
                alt="codeImage"
                class="code-image"
                @click="getCodeImage"/>
            </div>
            <div class="item">
              <span class="icon">
                <img src="../assets/login_6.png" alt="user">
              </span>
              <input 
                class="input" 
                type="text" 
                placeholder="请输入短信验证码"
                autocomplete="off"
                v-model.trim="loginForm.phoneCode"/>
                <span 
                  :class="['phoneCode',{'phoneCodeC':canSend, 'phoneCodeD': !canSend}]"
                  @click.stop="getPhoneCode">
                  {{verCodeButtonText}}
                </span>
            </div>
            <div class="item">
            <button
              class="login-btn"
              style="margin-bottom:14px;"
              type="button"
              @click.stop="handleLogin"
            >登 录</button>
            </div>
            <div class="item">
              <span class="ukey-login" @click="ukeyLogin">CA登录</span>
            </div>
          </div>
      </div>
    </div>
  </div>
</template>

<script>
import * as token from '@/utils/auth.js';
import { anonymousCaptcha, anonymousLogin, randomNum } from '@/http/apiUrls';
import { GZCA } from '@/utils/gzca.js';
export default {
  name: 'Login',
  data() {
    return {
      imageCode: "",
      verCodeButtonText: '获取短信验证码',
      waitingSecond: 60,
      canSend: true,
      loginForm: {
        userName: "",
        password: "",
        code: "",
        phoneCode: "",
        key:'',
        submitUserName: ''
      },
    }
  },
  mounted(){
    // undo测试用
    this.getCodeImage()
  },
  methods:{
    // 图形验证码
    getCodeImage(){
      this.$postJson(anonymousCaptcha, {}).then(res => {
        if(res){
          this.loginForm.key = res.key;
          this.imageCode = res.imgCode;
        }
      })
    },
    // 手机验证码
    getPhoneCode(){
      if(this.canSend == false){
        return;
      }
      if(this.loginForm.code=="" || this.loginForm.userName=="" || this.loginForm.password==""){
        this.$message({
          type: "error",
          message: "请将登录信息填写完整再获取短信验证码!",
        });
        return;
      }
      const requestData = {
        grantType: 'password',
        clientId: 'admin_web',
        key: this.loginForm.key,
        code: this.loginForm.code,
        userName: this.loginForm.userName,
        password: this.loginForm.password
      }
      this.canSend = false;
      this.verCodeButtonText = '发送中...'
      this.$postJson(anonymousLogin, requestData).then(res => {
        if(res){
          this.$message({
            type: "success",
            message: "验证码发送成功!",
          });
          this.verCodeButtonText = '已发送，请等待60'
          this.loginForm.submitUserName = requestData.userName
          this.interval = setInterval(() => this.tick(), 1000);
        }else{
          this.canSend = true;
          this.verCodeButtonText = '获取短信验证码'
          this.getCodeImage();
        }
      })
    },
    tick() {
      if(this.waitingSecond>0){
        let nowSeconds = this.waitingSecond-1;
        this.waitingSecond = nowSeconds
        this.verCodeButtonText = '已发送，请等待'+nowSeconds
      }else{
        this.canSend = true;
        this.waitingSecond = 60
        this.verCodeButtonText = '获取短信验证码'
        clearInterval(this.interval);
      }
    },
    // 登录
    handleLogin() {
      // undo测试用
      // token.set({
      //   token: 'test',
      //   userName: '3t'
      // })
      // this.$router.replace('/home');
      // 验证字段是否填写
      if(this.loginForm.phoneCode==""){
        this.$message({
          type: "error",
          message: "请先获取短信验证码!",
        });
        return;
      }
      
      const requestData = {
        grantType: 'sms',
        clientId: 'admin_web',
        userName: this.loginForm.submitUserName,
        code: this.loginForm.phoneCode
      }
      this.$postJson(anonymousLogin, requestData).then(res => {
        if(res){
          token.set(res)
          this.$router.replace('/home');
        }
      })
    },
    // CA登录
    ukeyLogin(){
      GZCA.init(res=>{
        if(!res){
          this.$message({
            type:"error",
            message:"连接失败",
          });
          return;
        }
        this.getUkeyList();
      });
    },
    // 获取证书列表
    getUkeyList(){
      const certType = 1;
      GZCA.GZCA_GetCertList(true, certType, res=>{
        if(!res.success){
          this.$message({
            type:"error",
            message:res.msg,
          });
          return;
        }
        this.getCert(res.ContainerName, certType);
      });
    },
    // 获取证书
    getCert(containerName, certType){
      GZCA.GZCA_ExportCert(containerName, certType, res=>{
        if(!res.success){
          this.$message({
            type:"error",
            message:res.msg,
          });
          return;
        }
        this.getSerial(res.CertB64, containerName);
      });
    },
    // 获取证书签名序列号
    getSerial(certB64,containerName){
      GZCA.GZCA_GetCertInfo(certB64, res=>{
        if(!res.success){
          this.$message({
            type:"error",
            message:res.msg,
          });
          return;
        }
        this.getRandom(res.CertSerial, containerName);
      });
    },
    // 获取随机数
    getRandom(certSerial, containerName){
      const requestData = {
        "signCertSerial": certSerial
      };
      this.$postJson(randomNum, requestData).then(res=>{
        if(res){
          let signData = res;
          console.log(containerName, signData, certSerial);
          this.Pkcs1SingData(containerName, signData, certSerial);
        }
      })
    },
    // 随机数签名
    Pkcs1SingData(containerName, signData, certSerial){
      GZCA.GZCA_Pkcs1SignData_PIN(containerName, signData, res=>{
        if(!res.success){
          this.$message({
            type:"error",
            message:res.msg,
          });
          return;
        }
        this.CALogin(res.SignData, certSerial);
      });
    },
    // 登录
    CALogin(signData, certSerial){
      const requestData ={
        "grantType":"ukey",
        "clientId":"admin_web",
        "signData":signData,
        "signCertSerial":certSerial
      };
      this.$postJson(anonymousLogin, requestData).then(res => {
        if(res){
          token.set(res)
          this.$router.replace('/home');
        }
      }); 
    },
  }
}
</script>
<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #aaa;
$light_gray: #eee;

.login-container {
  user-select: none;
  background: url(../assets/login_1.png) no-repeat;
  background-size: 100% 100%;
  background-position: center top;
  width: 100%;
  height: 100vh;
  display:flex;
  justify-content: center;
  align-items: center;
  .login-body {
    height: auto;
    border-radius: 22px;
    display: flex;
    background:#fff;
    box-shadow: 10px 10px 20px rgba(192,192,192,0.15);
    .left-container {
      width: 400px;
      height: 626px;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .right-container {
      padding: 60px 20px;
      flex: 1;
      height: 100%;
      .title {
        text-align: center;
        color: rgb(85,132,255);
        letter-spacing: 3px;
      }
      .sub-title {
        text-align: center;
        margin-top: 50px;
        letter-spacing: 2px;
        font-weight: normal;
      }
      .login-content {
        margin-top: 30px;
        position: relative;
        .item {
          height: 40px;
          margin: 0 auto;
          width: 70%;
          display:flex;
          justify-content:space-between;
          align-items:center;
          margin-bottom: 20px;
          position: relative;
          .icon {
            width: 14px;
            img {
              width: 100%;
              height: 100%;
            }
          }
          .input {
            padding-left: 8px;
            flex:1;
            height: 100%;
            margin-left: 8px;
            border: none;
            outline: none;
            border-bottom: 1px solid rgb(225,233,243);
          }
          ::-webkit-input-placeholder {
            color: rgb(144, 152, 180);
          }
          .code-image {
            width: 100px;
            height: 34px;
            border-radius: 4px;
            border: 1px solid rgb(225, 225, 226);
            margin-bottom: 2px;
            cursor: pointer;
            position:absolute;
            right: 0;
            bottom: 2px;
          }
          .phoneCode {
            font-size: 14px;
            cursor: pointer;
            position:absolute;
            right: 0;
          }
          .phoneCodeC{
            color: rgb(85,132,255);
          }
          .phoneCodeD{
            color: #a4b0be;
          }
          .login-btn{
            width: 500px;
            height: 42px;
            border-radius: 4px;
            background-color: rgb(85,132,255);
            border: none;
            cursor: pointer;
            color:#fff;
            text-align: center;
            display: block;
            margin: 80px auto 0;
          }
          .login-btn:hover {
            background-color:rgba(85,132,255,0.9);
          }
          .ukey-login {
            cursor: pointer;
            color: rgb(85,132,255);
            font-size: 14px;
            text-decoration: underline;
            position: absolute;
            right: 2px;
            bottom: -8px;
          }
          .ukey-login:hover {
            opacity: 0.8;
          }
        }
        
      }
     
    }
  }
}
</style>